<template>
  <div class="ebook">
    <ebook-reader></ebook-reader>
    <ebook-title></ebook-title>
    <ebook-menu></ebook-menu>
  </div>
</template>

<script type='text/ecmascript-6'>
import EbookReader from 'components/ebook/EbookReader'
import EbookTitle from 'components/ebook/EbookTitle'
import EbookMenu from 'components/ebook/EbookMenu'
import { getReadTime, saveReadTime } from 'utils/localStorage'
import { ebookMixin } from 'utils/mixin'

export default {
  mixins: [ebookMixin],
  components: {
    EbookReader,
    EbookTitle,
    EbookMenu
  },
  methods: {
    startLoopReadTime() {
      let readTime = getReadTime(this.fileName)
      if (!readTime) {
        readTime = 0
      }
      this.task = setInterval(() => {
        readTime++
        if (readTime % 30 === 0) {
          // 每30秒存储一次
          saveReadTime(this.fileName, readTime)
        }
      }, 1000);
    }
  },
  mounted() {
    this.startLoopReadTime()
  },
  beforeDestroy() {
    if (this.task) {
      clearInterval(this.task)
    }
  }
}
</script>

<style lang='scss' rel='stylesheet/scss' scoped>
@import '~assets/styles/global';
</style>
